package edu.sysu.cshare.dao;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.sun.org.apache.regexp.internal.recompile;

import edu.sysu.cshare.util.HibernateSessionFactory;
import edu.sysu.cshare.util.MD5;

@Entity
@Table(name = "T_USER")
public class User implements Serializable {

	public static User getUserById(String id) {
		return (User) HibernateSessionFactory.getSession().load(User.class, id);
	}

	private List<Comment> comments = new ArrayList<Comment>();

	private List<Course> courses = new ArrayList<Course>();

	private String descrip;

	private String id;

	private String lastIP;

	private List<Message> messages = new ArrayList<Message>();

	private String name;

	private String password;

	/**
	 * 1=普通学生 3=学委 4=老师或者助教 7=系统管理员 所有人都能登录并发布信息 第0位->能提交作业 第1位->能管理用户
	 * 第2位->能管理作业
	 */
	private int role;

	private List<SubmitInfo> submitInfos = new ArrayList<SubmitInfo>();
	public User() {

	}
	/**
	 * 简单的构造函数 默认学生身份 默认密码与学号相同,默认描述为空
	 * 
	 * @param id
	 *            学号
	 * @param name
	 *            姓名
	 */
	public User(String id, String name) {
		super();
		this.id = id;
		this.name = name;
		this.role = 1;
		this.password = MD5.getMD5(id);
	}
	/**
	 * 构造函数
	 * 
	 * @param id
	 *            一般是学号
	 * @param name
	 *            用户姓名
	 * @param password
	 *            明文密码,在构造函数中进行MD5加密
	 * @param descrip
	 *            描述
	 */
	public User(String id, String name, String password, int role,
			String descrip) {
		super();
		this.descrip = descrip;
		this.id = id;
		this.role = role;
		this.name = name;
		this.password = MD5.getMD5(password);
	}

	public void addCourse(Course c) {
		this.courses.add(c);
	}

	public void addCourses(List<Course> cous) {
		this.courses.addAll(cous);
	}
	public boolean canManagerHomework() {
		return ((role & 4) != 0);
	}
	public boolean canManagerUser() {
		return ((role & 2) != 0);
	}

	public boolean canShare() {
		return canSubmit();
	}
	public boolean canSubmit() {
		return ((role & 1) != 0);
	}

	public void delCourse(Course c) {
		this.courses.remove(c);
	}

	public void delCourses(List<Course> cous) {
		this.courses.removeAll(cous);
	}

	@OneToMany(mappedBy = "commenter")
	public List<Comment> getComments() {
		return comments;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", role=" + role + "]";
	}
	/**
	 * 指定Homework, 返回相应的SubmitInfo
	 * 
	 * @param homework
	 */
	public SubmitInfo getCorrespondingSubmitInfo(Homework homework) {
		for (SubmitInfo s : submitInfos) {
			if (s.getHomework().getId() == homework.getId())
				return s;
		}
		return null;
	}

	@ManyToMany(targetEntity = Course.class)
	@JoinTable(name = "TR_USER_COURSE", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "course_id", referencedColumnName = "id")

	)
	public List<Course> getCourses() {
		return courses;
	}

	public String getDescrip() {
		return descrip;
	}

	@Id
	@Column(length = 20)
	public String getId() {
		return id;
	}

	@Column(length = 15)
	public String getLastIP() {
		try {
			lastIP = InetAddress.getLocalHost().getHostAddress().toString();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return lastIP;

	}

	@OneToMany(mappedBy = "publisher")
	public List<Message> getMessages() {
		return messages;
	}

	@Column(length = 50)
	public String getName() {
		return name;
	}

	@Column(length = 50)
	public String getPassword() {
		return password;
	}

	public int getRole() {
		return role;
	}

	@OneToMany(mappedBy = "user")
	public List<SubmitInfo> getSubmitInfos() {
		return submitInfos;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}

	public void setDescrip(String descrip) {
		this.descrip = descrip;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setLastIP(String lastIP) {
		this.lastIP = lastIP;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public void setSubmitInfos(List<SubmitInfo> submitInfos) {
		this.submitInfos = submitInfos;
	}
}
